package com.jinlia.show.modules.base.wxapp.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.modules.base.system.entity.Goods;
import com.jinlia.show.modules.base.model.po.GoodsPO;
import com.jinlia.show.modules.base.system.service.GoodsMapperService;
import com.jinlia.show.modules.base.wxapp.entity.SearchHis;
import com.jinlia.show.modules.base.wxapp.mapper.SearchHisMapper;
import com.jinlia.show.modules.base.model.query.SearchHisQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SearchHisMapperService extends ServiceImpl<SearchHisMapper, SearchHis> {
    final GoodsMapperService goodsMapperService;
    public List<SearchHis> hotSearch(SearchHisQuery searchHisQuery) {
        Page<SearchHis> page = new Page<>(searchHisQuery.getCurrent(), searchHisQuery.getSize());
        Page<SearchHis> page1 = this.lambdaQuery().page(page);
        return page1.getRecords();
    }

    public List<GoodsPO> goodsSearch(SearchHis searchHis) {
        List<Goods> list = goodsMapperService.lambdaQuery()
                .like(Goods::getGoodsName, searchHis.getContent())
                .last("Limit 10")
                .list();
        List<GoodsPO> collectPO = list.stream().map(GoodsPO::fromEntity).collect(Collectors.toList());
        return collectPO;
    }
}
